/****************************************************************************
**
** This file is part of qPhotoTag, a photo keywording application
**
** Copyright (C) 2009 Marcell Lengyel <miketkf@gmail.com>
**
** GNU General Public License
** This program is free software; you can redistribute it and/or
** modify it under the terms of the GNU General Public License
** as published by the Free Software Foundation; either version 2
** of the License, or (at your option) any later version.
**
** This program is distributed in the hope that it will be useful,
** but WITHOUT ANY WARRANTY; without even the implied warranty of
** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
** GNU General Public License for more details.
**
** You should have received a copy of the GNU General Public License
** along with this program; if not, write to the Free Software
** Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
**
****************************************************************************/
#include <QMessageBox>
#include "gpxwpthandler.h"

GpxWptHandler::GpxWptHandler(QList<GpxWpt*> &wpt_list)
        : waypoint_list(&wpt_list)
{

}

bool GpxWptHandler::startElement(const QString & /* namespaceURI */,
                                const QString & /* localName */,
                                const QString &qName,
                                const QXmlAttributes &attributes)
{
    currentText.clear();
    if (qName == "wpt") {
        lat = attributes.value("lat").toDouble();
        lon = attributes.value("lon").toDouble();
        altitude = 0;
        name.clear();
    }
    // qDebug(qName.toAscii());
    return true;
}

 bool GpxWptHandler::endElement(const QString & /* namespaceURI */,
                              const QString & /* localName */,
                              const QString &qName)
 {
     if (qName == "name" && ! currentText.isEmpty())
         name = currentText;
     else if (qName == "cmt" && name.isEmpty())
         name = currentText;
     else if (qName == "desc" && name.isEmpty())
         name = currentText;
     else if (qName == "ele")
         altitude = currentText.toDouble();
     else if (qName == "wpt") {
         waypoint_item = new (GpxWpt);
         waypoint_item->altitude = altitude;
         waypoint_item->lat = lat;
         waypoint_item->lon = lon;
         waypoint_item->name = name;
         waypoint_list->append(waypoint_item);
     }
     return true;
 }

 bool GpxWptHandler::characters(const QString &str)
 {
     currentText += str;
     return true;
 }

 bool GpxWptHandler::fatalError(const QXmlParseException &exception)
 {
     QMessageBox::information(0, QObject::tr("GPX Waypoints"),
                              QObject::tr("Parse error at line %1, column %2:\n"
                                          "%3")
                              .arg(exception.lineNumber())
                              .arg(exception.columnNumber())
                              .arg(exception.message()));
     return false;
 }

 QString GpxWptHandler::errorString() const
 {
     return error_string;
 }
